package com.ruoyi.web.zangchat.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.zangchat.domain.ChatLogTalk;
import com.ruoyi.web.zangchat.service.IChatLogTalkService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 聊天记录Controller
 * 
 * @author ruoyi
 * @date 2020-07-29
 */
@Controller
@RequestMapping("/zangchat/talklog")
public class ChatLogTalkController extends BaseController
{
    private String prefix = "zangchat/talklog";

    @Autowired
    private IChatLogTalkService chatLogTalkService;

    @RequiresPermissions("zangchat:talklog:view")
    @GetMapping()
    public String talklog()
    {
        return prefix + "/talklog";
    }

    /**
     * 查询聊天记录列表
     */
    @RequiresPermissions("zangchat:talklog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String,Object> map)
    {
        startPage();
        return getDataTable(chatLogTalkService.logTalkList(map));
    }

    /**
     * 导出聊天记录列表
     */
    @RequiresPermissions("zangchat:talklog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChatLogTalk chatLogTalk)
    {
        List<ChatLogTalk> list = chatLogTalkService.list(new QueryWrapper<>());
        ExcelUtil<ChatLogTalk> util = new ExcelUtil<ChatLogTalk>(ChatLogTalk.class);
        return util.exportExcel(list, "talklog");
    }

    /**
     * 新增聊天记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存聊天记录
     */
    @RequiresPermissions("zangchat:talklog:add")
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChatLogTalk chatLogTalk)
    {
        return toAjax(chatLogTalkService.save(chatLogTalk));
    }

    /**
     * 修改聊天记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ChatLogTalk chatLogTalk = chatLogTalkService.getById(id);
        mmap.put("chatLogTalk", chatLogTalk);
        return prefix + "/edit";
    }

    /**
     * 修改保存聊天记录
     */
    @RequiresPermissions("zangchat:talklog:edit")
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChatLogTalk chatLogTalk)
    {
        return toAjax(chatLogTalkService.updateById(chatLogTalk));
    }

    /**
     * 删除聊天记录
     */
    @RequiresPermissions("zangchat:talklog:remove")
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chatLogTalkService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
